package cn.worldyao.demo.controller;


import cn.worldyao.demo.entity.Products;
import cn.worldyao.demo.service.ProductsService;
import cn.worldyao.demo.util.ReturnSet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yaozi
 * @since 2020-03-07
 */
@RestController
@RequestMapping("/products")
public class ProductsController {
    @Autowired
    private ProductsService productsService;

    @GetMapping("/list")
    public ReturnSet getProductsList() {
        List<Products> products = productsService.list();
        JSONArray array = JSONArray.parseArray(JSON.toJSONString(products));
        System.out.println(array);
        return ReturnSet.success(array);
    }

    @PostMapping(value = "/add")
    public ReturnSet addProduct(@RequestBody Products products) {
        boolean query = productsService.save(products);
        if (query) {
            return ReturnSet.success();
        }
        System.out.println(products);
        System.out.println(query);
        return ReturnSet.fail();
    }
}
